package com.qf.adminController;

import com.qf.adminService.AdminGoodsService;
import com.qf.pojo.Goods;
import com.qf.pojo.GoodsType;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.adminController
 * @Description:
 * @Date 2022/10/4 19:59
 */
@RestController
@RequestMapping("/admingoods")
public class AdminGoodsControler {

    @Autowired
    AdminGoodsService adminGoodsService;

    @RequestMapping("/getGoodsType")
    public ModelAndView getGoodsType(String flag, ModelAndView mav){
        if (flag.equals("show")){
            List<GoodsType> list = adminGoodsService.showAllGoodsTypes();
            mav.addObject("goodsTypeList",list);
            mav.setViewName("forward:/admin/showGoodsType.jsp");
            return mav;
        }
        if (flag.equals("add")){
            List<GoodsType> list = adminGoodsService.showAllGoodsTypes();
            mav.addObject("goodsTypeList",list);
            mav.setViewName("forward:/admin/addGoodsType.jsp");
            return mav;
        }
        return null;
    }

    @PostMapping("/addGoodsType")
    public ModelAndView addGoodsType(ModelAndView mav,String goodsParent,String typename){
        adminGoodsService.addType(typename,goodsParent);
        List<GoodsType> list = adminGoodsService.showAllGoodsTypes();
        mav.addObject("goodsTypeList",list);
        mav.setViewName("forward:/admin/showGoodsType.jsp");
        return mav;
    }

    @GetMapping("/getGoodsList")
    public ModelAndView getGoodsList(ModelAndView mav){
        List<Goods> goods = adminGoodsService.showAllGoods();
        mav.addObject("goodsList",goods);
        mav.setViewName("forward:/admin/showGoods.jsp");
        return mav;
    }

    @PostMapping("/addGoods")
    public ModelAndView addGoods(Goods goods, ModelAndView mav){
        adminGoodsService.addGood(goods);
        List<Goods> goodsList = adminGoodsService.showAllGoods();
        mav.addObject("goodsList",goodsList);
        mav.setViewName("forward:/admin/showGoods.jsp");
        return mav;
    }
}
